/*
 * CollisionManager.cpp
 *
 *  Created on: 02/09/2010
 *      Author: alex
 */

#include "CollisionManager.h"

using std::vector;


CollisionManager collisionManager;


void CollisionManager::add(CollisorType type, GameObject* collider) {
	colliders.push_back(CollisionPair(type, collider));
}


void CollisionManager::treatCollisions(unsigned dt) {
	for (vector<CollisionPair>::iterator it = colliders.begin(); it != colliders.end(); ++it) {
		for (vector<CollisionPair>::iterator jt = it + 1; jt != colliders.end(); ++jt) {
			if (it->collisor->collidesWith(*jt->collisor)) {
				if (it->type == COLLIDING_PLAYER && jt->type == COLLIDING_PLAYER)
					treatCollision((Player&)*it->collisor, (Player&)*jt->collisor, dt);
				else if (it->type == COLLIDING_PLAYER && jt->type == COLLIDING_FIRE)
					treatCollision((Player&)*it->collisor, (FireBall&)*jt->collisor, dt);
			}
		}
	}
	del:
	for (vector<CollisionPair>::iterator it = colliders.begin(); it != colliders.end(); ++it) {
		if (it->collisor->getLife() <= 0) {
			colliders.erase(it);
			goto del;
		}
	}
}

void CollisionManager::treatCollision(Player& a, Player& b, unsigned dt) {
	a.bounce(dt, b.getAngle(), b.getSpeed());
	b.bounce(dt, a.getAngle(), a.getSpeed());
}

void CollisionManager::treatCollision(Player& a, FireBall& b, unsigned  dt) {
	if (b.owner == &a) return;
	a.hit(10);
	b.hit(100);
	b.disable();
}
